package com.micrometa.km.ui.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class IconUtil {
	public static String ROOT = "resource/icons";
	public static String GIF = ".gif";
	public static String PNG = ".png";
	public static String DEFAULT_ERROR="resource/icons/error.png";

	public static Icon getIcon(String iconName) {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		URL url = cl.getResource(ROOT + iconName + GIF);
		Icon icon=getIcon(url);
		if (icon == null){
			url = cl.getResource(ROOT + iconName + PNG);
			icon=getIcon(url);
		}
		return icon;
	}
	public static Image getImage(String iconName) {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		Image image=Toolkit.getDefaultToolkit().createImage(ROOT + iconName + GIF);
		if (image == null){
			image = Toolkit.getDefaultToolkit().createImage(ROOT + iconName + PNG);
		}
		return image;
	}

	public static Icon getIcon(URL url) {
		if (url != null)
			return new ImageIcon(url);
		else
			return null;
	}
}
